/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.wx.statweb.action;

import cn.ac.iie.di.wx.statweb.utils.DateUtil;
import cn.ac.iie.di.wx.statweb.utils.PicCopy;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.logging.Level;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

/**
 *
 * @author Administrator
 */
public class NewsStatWebAction {

    public static Logger logger = Logger.getLogger(NewsStatWebAction.class.getName());

    public String news_rt() {
        return "success";
    }

    public String xw_jr_rt_data() {
        PrintWriter out = null;
        try {
            HttpServletRequest request = ServletActionContext.getRequest();
            String rt_msgdelay = request.getParameter("rt_msg");
            HttpServletResponse response = ServletActionContext.getResponse();
            response.reset();
            response.setContentType("text/html;charset=UTF-8");
            response.setCharacterEncoding("UTF-8");//防止弹出的信息出现乱码getjobinfo
            out = response.getWriter();
            //        logger.info(selectedDate);
//        File file = new File("E:\\images");
            File file = new File(PicCopy.readProperties("news.jr.head") + DateUtil.getToDayDate_1() + PicCopy.readProperties("news.jr.rt.tail"));
            File[] files = file.listFiles();
            String fileStrArray = "";
            //        logger.info(System.getProperty("user.dir"));
//        logger.info(files.length);
            for (int i = 0; i < files.length; i++) {
                String filename = files[i].getName();
//            String filePath = "E:\\ban-ycl-wb-stat-web\\src\\main\\webapp\\tencimages\\";
                String filePath = System.getProperty("user.dir") + "/" + PicCopy.readProperties("webapp.news.jr.path") + "8/";
                File fileDir = new File(filePath);
                if (!fileDir.isDirectory()) {
//                logger.info(fileDir.isDirectory());
                    fileDir.mkdirs();
//                logger.info(filePath);
                }
                File f = new File(filePath + filename);
                if (f.exists()) {
                    //删除原有的
                    if (f.delete()) {
                        f.createNewFile();
                    }
                    PicCopy.copyFile(files[i], f);
//                fileStrArray += filename + ";";
                } else {
                    f.createNewFile();
//                logger.info("create file");
                    PicCopy.copyFile(files[i], f);
//                fileStrArray += filename + ";";
                }
            }
            String[] fs = file.list();
            Arrays.sort(fs);
            for (int i = 0; i < fs.length; i++) {
                fileStrArray += fs[i] + ";";
            }
            out.print(fileStrArray);
        } catch (IOException ex) {
            logger.error(ex.getMessage(), ex);
        } finally {
            out.close();
        }
        return "success";
    }

    public String xw_rk_rt_data() throws IOException {
        HttpServletRequest request = ServletActionContext.getRequest();
        String rt_msgdelay = request.getParameter("rt_msg");
        HttpServletResponse response = ServletActionContext.getResponse();
        response.reset();
        response.setContentType("text/html;charset=UTF-8");
        response.setCharacterEncoding("UTF-8");//防止弹出的信息出现乱码getjobinfo
        PrintWriter out = response.getWriter();
//        logger.info(selectedDate);
//        File file = new File("E:\\images");
        File file = new File(PicCopy.readProperties("news.rk.head") + DateUtil.getToDayDate_1() + PicCopy.readProperties("news.rk.rt.tail"));
        File[] files = file.listFiles();
        String fileStrArray = "";
//        logger.info(System.getProperty("user.dir"));
//        logger.info(files.length);
        for (int i = 0; i < files.length; i++) {
            String filename = files[i].getName();
//            String filePath = "E:\\ban-ycl-wb-stat-web\\src\\main\\webapp\\tencimages\\";
            String filePath = System.getProperty("user.dir") + "/" + PicCopy.readProperties("webapp.news.rk.path") + "8/";
            File fileDir = new File(filePath);
            if (!fileDir.isDirectory()) {
//                logger.info(fileDir.isDirectory());
                fileDir.mkdirs();
//                logger.info(filePath);
            } else {
//                logger.info(fileDir.isDirectory());
//                logger.info(filePath);
            }
            File f = new File(filePath + filename);
            if (f.exists()) {
                //删除原有的
                if (f.delete()) {
                    f.createNewFile();
                }
                PicCopy.copyFile(files[i], f);
//                fileStrArray += filename + ";";
            } else {
                f.createNewFile();
//                logger.info("create file");
                PicCopy.copyFile(files[i], f);
//                fileStrArray += filename + ";";
            }
        }
        String[] fs = file.list();
        Arrays.sort(fs);
        for (int i = 0; i < fs.length; i++) {
            fileStrArray += fs[i] + ";";
        }
        out.print(fileStrArray);
        out.close();
        return "success";
    }

    public String xw_trs_rt_data() throws IOException {
        HttpServletRequest request = ServletActionContext.getRequest();
        String rt_msgdelay = request.getParameter("rt_msg");
        HttpServletResponse response = ServletActionContext.getResponse();
        response.reset();
        response.setContentType("text/html;charset=UTF-8");
        response.setCharacterEncoding("UTF-8");//防止弹出的信息出现乱码getjobinfo
        PrintWriter out = response.getWriter();
        File file = new File(PicCopy.readProperties("news.trs.head") + DateUtil.getToDayDate_1() + PicCopy.readProperties("news.trs.rt.tail"));
        File[] files = file.listFiles();
        String fileStrArray = "";
//        logger.info(System.getProperty("user.dir"));
//        logger.info(files.length);
        for (int i = 0; i < files.length; i++) {
            String filename = files[i].getName();
//            String filePath = "E:\\ban-ycl-wb-stat-web\\src\\main\\webapp\\tencimages\\";
            String filePath = System.getProperty("user.dir") + "/" + PicCopy.readProperties("webapp.news.trs.path") + "8/";
            File fileDir = new File(filePath);
            if (!fileDir.isDirectory()) {
//                logger.info(fileDir.isDirectory());
                fileDir.mkdirs();
//                logger.info(filePath);
            } else {
//                logger.info(fileDir.isDirectory());
//                logger.info(filePath);
            }
            File f = new File(filePath + filename);
            if (f.exists()) {
                //删除原有的
                if (f.delete()) {
                    f.createNewFile();
                }
                PicCopy.copyFile(files[i], f);
//                fileStrArray += filename + ";";
            } else {
                f.createNewFile();
//                logger.info("create file");
                PicCopy.copyFile(files[i], f);
//                fileStrArray += filename + ";";
            }
        }
        String[] fs = file.list();
        Arrays.sort(fs);
        for (int i = 0; i < fs.length; i++) {
            fileStrArray += fs[i] + ";";
        }
        out.print(fileStrArray);
        out.close();
        return "success";
    }

    public String xw_mpp_rt_data() throws IOException {
        HttpServletRequest request = ServletActionContext.getRequest();
        String rt_msgdelay = request.getParameter("rt_msg");
        HttpServletResponse response = ServletActionContext.getResponse();
        response.reset();
        response.setContentType("text/html;charset=UTF-8");
        response.setCharacterEncoding("UTF-8");//防止弹出的信息出现乱码getjobinfo
        PrintWriter out = response.getWriter();
        File file = new File(PicCopy.readProperties("news.mpp.head") + DateUtil.getToDayDate_1() + PicCopy.readProperties("news.mpp.rt.tail"));
        File[] files = file.listFiles();
        String fileStrArray = "";
//        logger.info(System.getProperty("user.dir"));
//        logger.info(files.length);
        for (int i = 0; i < files.length; i++) {
            String filename = files[i].getName();
//            String filePath = "E:\\ban-ycl-wb-stat-web\\src\\main\\webapp\\tencimages\\";
            String filePath = System.getProperty("user.dir") + "/" + PicCopy.readProperties("webapp.news.mpp.path") + "8/";
            File fileDir = new File(filePath);
            if (!fileDir.isDirectory()) {
//                logger.info(fileDir.isDirectory());
                fileDir.mkdirs();
//                logger.info(filePath);
            } else {
//                logger.info(fileDir.isDirectory());
//                logger.info(filePath);
            }
            File f = new File(filePath + filename);
            if (f.exists()) {
                //删除原有的
                if (f.delete()) {
                    f.createNewFile();
                }
                PicCopy.copyFile(files[i], f);
//                fileStrArray += filename + ";";
            } else {
                f.createNewFile();
//                logger.info("create file");
                PicCopy.copyFile(files[i], f);
//                fileStrArray += filename + ";";
            }
        }
        String[] fs = file.list();
        Arrays.sort(fs);
        for (int i = 0; i < fs.length; i++) {
            fileStrArray += fs[i] + ";";
        }
        out.print(fileStrArray);
        out.close();
        return "success";
    }

    public String xw_daily_data() throws IOException {
        HttpServletRequest request = ServletActionContext.getRequest();
        String selectedDate = request.getParameter("xw_daily_data");
        String head = request.getParameter("head");
        String tail = request.getParameter("tail");
        String webpath = request.getParameter("webpath");
        //根据日期选择图片并
        HttpServletResponse response = ServletActionContext.getResponse();
        response.reset();
        response.setContentType("text/html;charset=UTF-8");
        response.setCharacterEncoding("UTF-8");//防止弹出的信息出现乱码getjobinfo
        PrintWriter out = response.getWriter();
//        logger.info(selectedDate);
//        File file = new File("E:\\images");
//        File file = new File("/home/ycl/plot/new/plot_test/pic/sina/" + selectedDate + "/24");
        File file = new File(PicCopy.readProperties(head) + selectedDate + PicCopy.readProperties(tail));
        logger.info(PicCopy.readProperties(head) + selectedDate + PicCopy.readProperties(tail));
        if (file.isDirectory() && file.exists()) {
            logger.info(PicCopy.readProperties(head) + selectedDate + PicCopy.readProperties(tail));

            File[] files = file.listFiles();
            String fileStrArray = "";
            for (int i = 0; i < files.length; i++) {
                String filename = files[i].getName();
//                logger.info(filename);
//                String filePath = "E:\\ban-ycl-wb-stat-web\\src\\main\\webapp\\images\\";
                String filePath = System.getProperty("user.dir") + "/" + PicCopy.readProperties(webpath) + selectedDate + "/24/";
                File fileDir = new File(filePath);
                if (!fileDir.isDirectory()) {
//                    logger.info(fileDir.isDirectory());
                    fileDir.mkdirs();
                    logger.info(filePath);
                } else {
//                    logger.info(fileDir.isDirectory());
                    logger.info(filePath);
                    //清空原有的文件

                }
                File f = new File(filePath + filename);
                if (f.exists()) {
                    PicCopy.copyFile(files[i], f);
//                    fileStrArray += filename + ";";
                } else {
                    f.createNewFile();
//                logger.info("create file");
                    PicCopy.copyFile(files[i], f);
//                    fileStrArray += filename + ";";
                }
            }
            String[] fs = file.list();
            Arrays.sort(fs);
            for (int i = 0; i < fs.length; i++) {
                fileStrArray += fs[i] + ";";
            }
            out.print(fileStrArray);
            out.close();
            return "success";
        } else {
            out.print("");
            out.close();
            return "success";
        }
    }

    public String xw_weekly_data() throws IOException {
        HttpServletRequest request = ServletActionContext.getRequest();
        String selectedDate = request.getParameter("xw_weekly_data");
        String head = request.getParameter("head");
        String tail = request.getParameter("tail");
        String webpath = request.getParameter("webpath");
        //根据日期选择图片并
        HttpServletResponse response = ServletActionContext.getResponse();
        response.reset();
        response.setContentType("text/html;charset=UTF-8");
        response.setCharacterEncoding("UTF-8");//防止弹出的信息出现乱码getjobinfo
        PrintWriter out = response.getWriter();
//        logger.info(selectedDate);
//        File file = new File("E:\\images");
//        File file = new File("/home/ycl/plot/new/plot_test/pic/sina/" + selectedDate + "/168");
        File file = new File(PicCopy.readProperties(head) + selectedDate + PicCopy.readProperties(tail));

        if (file.isDirectory() && file.exists()) {

            File[] files = file.listFiles();
            String fileStrArray = "";
//            logger.info(System.getProperty("user.dir"));
//            logger.info(files.length);
            for (int i = 0; i < files.length; i++) {
                String filename = files[i].getName();
//                String filePath = "E:\\ban-ycl-wb-stat-web\\src\\main\\webapp\\images\\";
//                String filePath = System.getProperty("user.dir") + "/" + "webapps/ban-ycl-wx-stat-web-1.0/sinaimages/" + selectedDate + "/168/";
                String filePath = System.getProperty("user.dir") + "/" + PicCopy.readProperties(webpath) + selectedDate + "/168/";

                File fileDir = new File(filePath);
                if (!fileDir.isDirectory()) {
//                    logger.info(fileDir.isDirectory());
                    fileDir.mkdirs();
//                    logger.info(filePath);
                } else {
//                    logger.info(fileDir.isDirectory());
//                    logger.info(filePath);
                    //清空原有的文件

                }
                File f = new File(filePath + filename);
                if (f.exists()) {
                    PicCopy.copyFile(files[i], f);
//                    fileStrArray += filename + ";";
                } else {
                    f.createNewFile();
//                logger.info("create file");
                    PicCopy.copyFile(files[i], f);
//                    fileStrArray += filename + ";";
                }
            }
            String[] fs = file.list();
            Arrays.sort(fs);
            for (int i = 0; i < fs.length; i++) {
                fileStrArray += fs[i] + ";";
            }
            out.print(fileStrArray);
            out.close();
            return "success";
        } else {
            logger.info("there is no this fileDirectory or File");
            out.print("");
            out.close();
            return "success";
        }
    }
}
